import EngineUtil from "./EngineUtil";
import n from "./EngineUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class AudioManager extends cc.Component {
    @property(cc.Integer)
    bgOpen: number = 0;
    @property(cc.Integer)
    effectOpen: number = 0;
    @property(cc.Integer)
    vibratorOpen: number = 0;

    private _bgAudios: { [key: string]: any } = {};
    private _effectAudio: { [key: string]: any } = {};
    private _effectAudioisPlayArray: { [key: string]: boolean } = {};

    static _instance: AudioManager = null;

    constructor() {
        super();
        if (AudioManager._instance) {
            return AudioManager._instance;
        }
        this.bgOpen = 0;
        this.effectOpen = 0;
        this.vibratorOpen = 0;
        this._bgAudios = {};
        this._effectAudio = {};
        this._effectAudioisPlayArray = {};
        AudioManager._instance = this;
    }

    static getInstance(): AudioManager {
        if (!AudioManager._instance) {
            AudioManager._instance = new AudioManager();
        }
        return AudioManager._instance;
    }

    init(): void {
        this._effectAudioisPlayArray = {};
        this.bgOpen = Number(EngineUtil.localStorageGetItem("bg_audio", "1"));
        this.effectOpen = Number(EngineUtil.localStorageGetItem("effect_audio", "1"));
        this.vibratorOpen = Number(EngineUtil.localStorageGetItem("vibratorOpen", "1"));
    }

    getMusicState(): boolean {
        return this.bgOpen === 1;
    }

    getAudioState(): boolean {
        return this.effectOpen === 1;
    }

    getVibratorState(): boolean {
        return this.vibratorOpen === 1;
    }

    openVibrator(): void {
        n.localStorageSetItem("vibratorOpen", "1");
        this.vibratorOpen = 1;
    }

    closeVibrator(): void {
        n.localStorageSetItem("vibratorOpen", "0");
        this.vibratorOpen = 0;
    }

    openAudio(): void {
        n.localStorageSetItem("effect_audio", "1");
        this.effectOpen = 1;
    }

    closeAudio(): void {
        n.localStorageSetItem("effect_audio", "0");
        this.effectOpen = 0;
    }

    openBg(): void {
        n.localStorageSetItem("bg_audio", "1");
        this.bgOpen = 1;
    }

    closeBg(): void {
        n.localStorageSetItem("bg_audio", "0");
        this.bgOpen = 0;
    }

    private _effectAudioPlayStatus = [];
    playMusic(soundName: string, isBackground: boolean = false, isLoop: boolean = false, callback: Function = null, isEffect: boolean = false): void {
        isLoop = isLoop !== false;
        if ((this.effectOpen !== 0 || isLoop) && (this.bgOpen !== 0 || !isLoop)) {
            isBackground = isBackground !== false;
            let path = "sound/" + soundName;
            let self = this;
            // cc.assetManager.loadRemote('http://www.cloud.com/test2.mp3', (err, audioClip) => console.log(err));
            cc.loader.loadRes(path, cc.AudioClip, function (error, clip) {
                if(error){
                    console.error(error);
                    return;
                }
                if (isLoop) {
                    self._bgAudios[soundName] = cc.audioEngine.playMusic(clip, isBackground);
                    if (callback) callback(self._bgAudios[soundName]);
                } else {
                    if (self._effectAudioPlayStatus["isPlaying_" + soundName] && !isEffect) return;
                    self._effectAudioPlayStatus["isPlaying_" + soundName] = true;
                    let duration = 1000//clip.duration;
                    self.scheduleOnce(function () {
                        self._effectAudioPlayStatus["isPlaying_" + soundName] = false;
                    }, duration);
                    try {
                        self._effectAudio[soundName] = cc.audioEngine.playEffect(clip, isBackground);
                    } catch (error) {
                        console.log("Error playing sound effect", error, isBackground);
                    }
                    if (callback) callback(self._effectAudio[soundName]);
                }
            });
        }
    }

    stopMusic(e: string, t: boolean) {
        if (t !== false) {
            cc.audioEngine.stopMusic();
        } else {
            cc.audioEngine.stop(this._effectAudio[e]);
        }
    }

    pauseMusic(e: string, t: boolean) {
        if (t !== false) {
            cc.audioEngine.pauseMusic();
        } else {
            cc.audioEngine.pause(this._effectAudio[e]);
            this._effectAudioisPlayArray["isPlay_" + e] = false;
        }
    }

    resumeMusic(e: string, t: boolean) {
        if (t !== false) {
            if (this.bgOpen === 0) return;
            cc.audioEngine.resumeMusic();
        } else {
            if (this.effectOpen === 0) return;
            cc.audioEngine.resume(this._effectAudio[e]);
        }
    }
}